<?php 
include("helper/DBOperation.php");
include("alert_loader.php");
echo "Last Ping CPU Time = ".$lastPingYellowCPU."<br>";
$dbObj = new DBOperation();
function insertInToCPU($cId){
	  global $lastPingYellowCPU, $lastPingRedCPU, $cpuTempYellowLow, $cpuTempYellowHigh, $ntwspdYellowLow, $ntwspdYellowHigh, $ntwspdRed, $hdspaceYellowLow, $hdspaceYellowHigh, $hdspaceRed;

	  $dbObj = new DBOperation();
 	  $sql = $dbObj->executeQuery("SELECT * FROM tbl_component_cpu where component_id='".$cId."' ORDER BY id DESC LIMIT 1", true);

	if($sql['last_ping']){
		$currentPingTime 	= strtotime(date("Y-m-d H:i:s"));
		$lastPingTime		= strtotime($sql['last_ping']) ;
		$pingDifference		= $currentPingTime - $lastPingTime ;
		$pingDifference		= round($pingDifference/60);
		//minutes to hour minute	
		$hours          = floor($pingDifference / 60);
		$decimalMinutes = $pingDifference - floor($pingDifference/60) * 60;

		# Put it together.
		$hoursMinutes = sprintf("%dH%02.0fM", $hours, $decimalMinutes);
		echo "Last Ping CPU Time = ".$lastPingYellowCPU."<br>";
		
		$stripColor = "green";
		$color = "green";
		
		
		if($pingDifference > $lastPingYellowCPU) {
			if($pingDifference <= $lastPingRedCPU){
				$color_status = 2;
				$message =  "last ping was : ".$hoursMinutes." ago";
				$color = "yellow";
				$stripColor	= "yellow";
			}
			else{
				$color_status = 3;
				$message ="last ping was : ".$hoursMinutes." ago";
				$color = "red";
				$stripColor	= "red";
			}		

			$insert_keyvalue['id'] = NULL;
			$insert_keyvalue['component_id'] = $sql['component_id'];
			$insert_keyvalue['component_type'] = $sql['component_type'];
			$insert_keyvalue['cpu_name'] = $sql['cpu_name'];
			$insert_keyvalue['ip_address'] = $sql['ip_address'];
			$insert_keyvalue['last_ping'] = $sql['last_ping'];
			$insert_keyvalue['current_status'] = "offline";
			$insert_keyvalue['cpu_temp'] = $sql['cpu_temp'];
			$insert_keyvalue['network_speed'] = $sql['network_speed'];
			$insert_keyvalue['os_version'] =$sql['os_version'];
			$insert_keyvalue['player_vers'] = $sql['player_vers'];
			$insert_keyvalue['last_reboot'] = $sql['last_reboot'];
			$insert_keyvalue['free_hd_space'] =$sql['free_hd_space'];
			$insert_keyvalue['schedule_name'] = $sql['schedule_name'];
			$insert_keyvalue['last_sched_upload'] = $sql['last_sched_upload'];
			$insert_keyvalue['schedule_file_size'] = $sql['schedule_file_size'];
			$dbObj->insertRecords('tbl_component_cpu',$insert_keyvalue);
			$unit_id = mysql_insert_id();

			if($message!="") {
				$insert_keyvalue_message = array();
				$insert_keyvalue_message['id'] = NULL;
				$insert_keyvalue_message['component_id'] = $sql['component_id'];
				$insert_keyvalue_message['unit_id'] = $unit_id;
				$insert_keyvalue_message['created_date'] = date("Y-m-d H:i:s");
				$insert_keyvalue_message['message'] = $message;
				$insert_keyvalue_message['status'] = $color_status;
				$dbObj->insertRecords('tbl_log_message', $insert_keyvalue_message);
			}
			$insert_keyvalue_state = array();
			$insert_keyvalue_state['id'] = NULL;
			$insert_keyvalue_state['component_id'] = $sql['component_id'];
			$insert_keyvalue_state['state'] = $stripColor;
			$insert_keyvalue_state['datetime'] = date("Y-m-d h:i:s");
			$dbObj->insertRecords('tbl_component_state',$insert_keyvalue_state);
			echo "CPU State - ".$stripColor."<br>";
		}
	}
}


function insertInToDisplay($cId,$display_type){
	
	global $lastPingYellowDisplay, $lastPingRedDisplay, $disTempYellowLow, $disTempYellowHigh, $backTempYellowLow, $backTempYellowHigh, $ctrlTempYellowLow, $ctrlTempYellowHigh, $dutyCycleYellow, $dispBrightYellowLow, $dispBrightYellowHigh, $dispBrightRed;
	
	$dbObj = new DBOperation();
 	$sql = $dbObj->executeQuery("SELECT * FROM tbl_component_display where component_id='".$cId."' AND display_type = ".$display_type."  ORDER BY id DESC LIMIT 1", true);
	if($sql['last_ping']){
		$currentPingTime 	= strtotime(date("Y-m-d H:i:s"));
		$lastPingTime		= strtotime($sql['last_ping']) ;
		$pingDifference		= $currentPingTime - $lastPingTime ;
		$pingDifference		= round($pingDifference/60);
		//minutes to hour minute	
		$hours          = floor($pingDifference / 60);
		$decimalMinutes = $pingDifference - floor($pingDifference/60) * 60;

		$hoursMinutes = sprintf("%dH%02.0fM", $hours, $decimalMinutes);
		
		$color = "green";

		if($pingDifference > $lastPingYellowDisplay) {
			if($pingDifference <= $lastPingRedDisplay){
				$color_status = 2;
				$message =  "last ping was : ".$hoursMinutes." ago";
				$color = "yellow";
			}
			else{
				$color_status = 3;
				$message ="last ping was : ".$hoursMinutes." ago";
				$color = "red";
			}		

			$insert_keyvalue['id'] = NULL;
			$insert_keyvalue['component_id'] = $sql['component_id'];
			$insert_keyvalue['display_type'] = $sql['display_type'];
			$insert_keyvalue['display_name'] = $sql['display_name'];
			$insert_keyvalue['ip_address'] =$sql['ip_address'];
			$insert_keyvalue['last_ping'] = $sql['last_ping'];
			$insert_keyvalue['current_status'] = "offline";
			$insert_keyvalue['display_temp'] = $sql['display_temp'];
			$insert_keyvalue['backlight_temp'] = $sql['backlight_temp'];
			$insert_keyvalue['ctrl_board_temp'] = $sql['ctrl_board_temp'];
			$insert_keyvalue['duty_cycle'] = $sql['duty_cycle'];
			$insert_keyvalue['ambient_light'] = $sql['ambient_light'];
			$insert_keyvalue['brightness'] =$sql['brightness'];
			$insert_keyvalue['input_source'] = $sql['input_source'];
			$dbObj->insertRecords('tbl_component_display',$insert_keyvalue);
			$unit_id = mysql_insert_id();

			$insert_keyvalue_message = array();
			$insert_keyvalue_message['id'] = NULL;
			$insert_keyvalue_message['component_id'] = $sql['component_id'];
			$insert_keyvalue_message['unit_id'] = $unit_id;
			$insert_keyvalue_message['created_date'] = date("Y-m-d h:i:s");
			$insert_keyvalue_message['message'] = $message;
			$insert_keyvalue_message['status'] = $color_status;
			$dbObj->insertRecords('tbl_log_message', $insert_keyvalue_message);

			$insert_keyvalue_state = array();
			$insert_keyvalue_state['id'] = NULL;
			$insert_keyvalue_state['component_id'] = $sql['component_id'];
			$insert_keyvalue_state['state'] = $color;
			$insert_keyvalue_state['datetime'] = date("Y-m-d h:i:s");
			$dbObj->insertRecords('tbl_component_state',$insert_keyvalue_state);
			echo "Display State - ".$color."<br>";
		}	

	}
}

function insertInToHVAC($cId){
	
	global $lastPingYellowHVAC, $lastPingRedHVAC, $inTempYellowLow, $inTempYellowHigh, $outTempYellowLow, $outTempYellowHigh, $inHumidityYellowLow, $inHumidityYellowHigh;
	
	$dbObj = new DBOperation();
 	$sql = $dbObj->executeQuery("SELECT * FROM tbl_component_hvac where component_id='".$cId."' ORDER BY id DESC LIMIT 1", true);
	if($sql['last_ping']){
		$currentPingTime 	= strtotime(date("Y-m-d H:i:s"));
		$lastPingTime		= strtotime($sql['last_ping']) ;
		$pingDifference		= $currentPingTime - $lastPingTime ;
		$pingDifference		= round($pingDifference/60);
		//minutes to hour minute	
		$hours          = floor($pingDifference / 60);
		$decimalMinutes = $pingDifference - floor($pingDifference/60) * 60;

		$hoursMinutes = sprintf("%dH%02.0fM", $hours, $decimalMinutes);

		$color = "green";
		
		if($pingDifference > $lastPingYellowHVAC) {
			if($pingDifference <= $lastPingRedHVAC){
				$color_status = 2;
				$message =  "last ping was : ".$hoursMinutes." ago";
				$color = "yellow";
			}
			else{
				$color_status = 3;
				$message ="last ping was : ".$hoursMinutes." ago";
				$color = "red";
			}		

			$insert_keyvalue['id'] = NULL;
			$insert_keyvalue['component_id'] =$cId ;
			$insert_keyvalue['hvac_unit_name'] = $sql['hvac_unit_name'];
			$insert_keyvalue['controller_firmware_ver'] = $sql['controller_firmware_ver'];
			$insert_keyvalue['ac_status'] = $sql['ac_status'];
			$insert_keyvalue['heater_status'] = $sql['heater_status'];
			$insert_keyvalue['water_present'] = $sql['water_present'];
			$insert_keyvalue['last_ping'] = $sql['last_ping'];
			$insert_keyvalue['inside_temp'] = $sql['inside_temp'];
			$insert_keyvalue['outside_temp'] = $sql['outside_temp'];
			$insert_keyvalue['inside_humidity'] = $sql['inside_humidity'];
	
			$dbObj->insertRecords('tbl_component_hvac',$insert_keyvalue);
			$unit_id = mysql_insert_id();
	
			$insert_keyvalue_message = array();
			$insert_keyvalue_message['id'] = NULL;
			$insert_keyvalue_message['component_id'] = $cId;
			$insert_keyvalue_message['unit_id'] = $unit_id;
			$insert_keyvalue_message['created_date'] = date("Y-m-d H:i:s");
			$insert_keyvalue_message['message'] = $message;
			$insert_keyvalue_message['status'] = $color_status;
			$dbObj->insertRecords('tbl_log_message', $insert_keyvalue_message);
		
			$insert_keyvalue_state = array();

			$insert_keyvalue_state['id'] = NULL;
			$insert_keyvalue_state['component_id'] = $cId;
			$insert_keyvalue_state['state'] = $color;
			$insert_keyvalue_state['datetime'] = date("Y-m-d H:i:s");

			$dbObj->insertRecords('tbl_component_state',$insert_keyvalue_state);
			echo "HVAC State - ".$color."<br>";
		}
	}
}


echo "\n\nCron Start at ==".date('d-m-Y H:i')."==";
$query = "select id,component_name from tbl_components";
$componentsResult = mysql_query($query);
if(mysql_num_rows($componentsResult) > 0){
	while($component = mysql_fetch_assoc($componentsResult)){
		switch($component['component_name']){
			case 'Displays':
					for($i=0;$i<5;$i++){
						insertInToDisplay($component['id'],$i);
					}
					break;
			case 'CPUH':
			case 'CPUV':
					insertInToCPU($component['id']);
					break;

			case 'HVAC-H':
			case 'HVAC-V':
					insertInToHVAC($component['id']);
					break;
		}
	}
} 
echo "\n\nCron end at ==".date('d-m-Y H:i')."==";
?>
